package gdut.jmxywz.action;

import gdut.jmxywz.been.BaseFunction;
import gdut.jmxywz.been.RoleFunction;
import gdut.jmxywz.been.Roles;
import gdut.jmxywz.been.TeacherRole;
import gdut.jmxywz.util.PageModel;

import java.io.UnsupportedEncodingException;
import java.util.List;

public class RoleFunctionAction extends BaseAction {

	private BaseFunction baseFunction;
	
	private List baseFunctionList;
	
	private List functionSelectList;
	
	private Roles roles;
	
	private RoleFunction roleFunction;
	
	private int roleId;
	
	private RoleFunction roleFunctionSubUpdate;
	
	private int roleFunctionId;
	
	/*--------------分页---------------------*/
	private PageModel pageModel;
	
	private int currentPage=1;
	
	private int pageSize=5;
	
	/*------------------检索---------------------*/
	private String searchSelect="";
	
	private String like="like";
	
	private String searchValue="";
	
	/*----------------geter/seter-----------------------*/
	public BaseFunction getBaseFunction() {
		return baseFunction;
	}

	public void setBaseFunction(BaseFunction baseFunction) {
		this.baseFunction = baseFunction;
	}
	
    public List getBaseFunctionList() {
		return baseFunctionList;
	}

	public void setBaseFunctionList(List baseFunctionList) {
		this.baseFunctionList = baseFunctionList;
	}
	
	
	public List getFunctionSelectList() {
		return functionSelectList;
	}

	public void setFunctionSelectList(List functionSelectList) {
		this.functionSelectList = functionSelectList;
	}
	

	public Roles getRoles() {
		return roles;
	}

	public void setRoles(Roles roles) {
		this.roles = roles;
	}
	

	public RoleFunction getRoleFunction() {
		return roleFunction;
	}

	public void setRoleFunction(RoleFunction roleFunction) {
		this.roleFunction = roleFunction;
	}
	

	public PageModel getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}
	
	public RoleFunction getRoleFunctionSubUpdate() {
		return roleFunctionSubUpdate;
	}

	public void setRoleFunctionSubUpdate(RoleFunction roleFunctionSubUpdate) {
		this.roleFunctionSubUpdate = roleFunctionSubUpdate;
	}

	public int getRoleFunctionId() {
		return roleFunctionId;
	}

	public void setRoleFunctionId(int roleFunctionId) {
		this.roleFunctionId = roleFunctionId;
	}
	
	public String getSearchSelect() {
		return searchSelect;
	}

	public void setSearchSelect(String searchSelect) {
		this.searchSelect = searchSelect;
	}

	public String getLike() {
		return like;
	}

	public void setLike(String like) {
		this.like = like;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue)throws UnsupportedEncodingException {
		this.searchValue = new String(searchValue.getBytes("ISO-8859-1"),"utf-8");
	}

/******************************************************************************/
	public String preAdd() throws Exception {
		// TODO Auto-generated method stub
		/*在增加页面中显现所有功能，用于被角色使用*/
		String hql="from BaseFunction";
		baseFunctionList=this.getCommonService().findListByHql(hql, null);
		if(baseFunctionList==null)
			return "error";
		else
			return SUCCESS;
	}


	public String add() throws Exception {
		// TODO Auto-generated method stub
		/*增加角色*/
		boolean b=this.getCommonService().saveObject(roles);
		if(!b)return "error";
		/*增加角色功能对应*/
	   for(int i=1;i<functionSelectList.size();i++){
		    String hql="from Roles r where r.roleName='"+roles.getRoleName()+"'";
	   		roleFunction=new RoleFunction();
	   		Roles r=new Roles();
	   		BaseFunction bf=new BaseFunction();
	   		List l=this.getCommonService().findListByHql(hql, null);
	   		if(l==null){
	   			return "error";
	   		}
	   		r=(Roles)l.get(0);
	   		roleFunction.setRoles(r);
	   		bf=(BaseFunction)this.getCommonService().findByIntId(Integer.parseInt(functionSelectList.get(i).toString()), "gdut.jmxywz.been.BaseFunction");
	   		if(bf==null){
	   			return "error";
	   		}
	   		roleFunction.setBaseFunction(bf);
	   		roleFunction.setRoleFunctionName(bf.getFunctionName());
	   		roleFunction.setRoleDescripe(bf.getFunctionDescripe());
	   		boolean bl=this.getCommonService().saveObject(roleFunction);
	   		 if(!bl){
	   			 return "error";
	   		 }
	   }
	   return SUCCESS;
	}

	
	public String show() throws Exception {
		// TODO Auto-generated method stub
		String hql="select r from Roles r";
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"roleFunctionShow.action","");
		if(pageModel.getList()==null){
			System.out.println("=================角色功能列表为空================");
		}
		return SUCCESS;
	}

	
	public String search() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Roles";
		String searchParameter="";
		
		String searchValue1=searchValue;
		
		if (like.equals("like")) {
			hql = hql + " as bf  where bf." + searchSelect + " like '%"+ searchValue1 + "%'";
		} else {
			hql = hql + " as bf  where bf." + searchSelect + " ='"+ searchValue1 + "'";
		}
		
		String enSearchValue=java.net.URLEncoder.encode(searchValue1,"UTF-8");//URL编码转换
		searchParameter="&searchSelect="+searchSelect+"&searchValue="+enSearchValue+"&like="+like;
		
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"roleFunctionSearch.action",searchParameter);
		System.out.print(hql);
		System.out.print(pageModel);
		
		return SUCCESS;
		
	}


	public String detail() throws Exception {
		// TODO Auto-generated method stub
		
		roles=(Roles)this.getCommonService().findByIntId(roleId, "gdut.jmxywz.been.Roles");
		
		//String hql="select rf from Roles r right outer join r.roleFunctions rf where r.roleId="+roleId+"";这种形式也可以
		
		String hql="select rf from RoleFunction rf where rf.roles.roleId="+roleId+"";
		
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"roleFunctionDetail.action","&roleId="+roleId);
		if(roles==null)
			return "error";
		else
			return SUCCESS;
		
	}

	
	public String preUpdate() throws Exception {
		// TODO Auto-generated method stub
		roles=(Roles)this.getCommonService().findByIntId(roleId, "gdut.jmxywz.been.Roles");
		/*未分配的功能*/
		String hqlNotSelected="select bf from BaseFunction bf where bf not in (select rf.baseFunction from RoleFunction rf where rf.roles.roleId="+roleId+") order by displayOrder";
		baseFunctionList=this.getCommonService().findListByHql(hqlNotSelected, null);
		/*已分配的功能*/
		String hqlSelected="select rf.baseFunction from RoleFunction rf where rf.roles.roleId="+roleId+" order by displayOrder";
		functionSelectList=this.getCommonService().findListByHql(hqlSelected, null);
		
		if(baseFunctionList==null&&functionSelectList==null)
			return "error";
		else
			return SUCCESS;
		
	}

	
	public String update() throws Exception {
		// TODO Auto-generated method stub
		/*修改角色*/
		boolean b=this.getCommonService().updateObject(roles);
		/**
		 * 修改角色功能对应，先删所有相应关联，再重新增加关联
		 * */
		/*删原来的角色功能对应关联*/
		String deleteList="select rf from RoleFunction rf where rf.roles.roleId="+roles.getRoleId();
		List olds=this.getCommonService().findListByHql(deleteList, null);
		for(int i=0;i<olds.size();i++){
			boolean d=this.getCommonService().deleteObject((RoleFunction)olds.get(i));
			if(!d){
				return "error";
			}
		}
		/*重增角色功能对应*/
		Roles r=new Roles();
		r=(Roles)this.getCommonService().findByIntId(roles.getRoleId(), "gdut.jmxywz.been.Roles");
		if(r==null){
			return "error";
		}
		
		   for(int i=1;i<functionSelectList.size();i++){
			    RoleFunction roleFunction=new RoleFunction();
			    BaseFunction bf=new BaseFunction();
		   		roleFunction.setRoles(r);
		   		bf=(BaseFunction)this.getCommonService().findByIntId(Integer.parseInt(functionSelectList.get(i).toString()), "gdut.jmxywz.been.BaseFunction");
		   		if(bf==null){
		   			return "error";
		   		}
		   		roleFunction.setBaseFunction(bf);
		   		roleFunction.setRoleFunctionName(bf.getFunctionName());
		   		roleFunction.setRoleDescripe(bf.getFunctionDescripe());
		   		boolean bl=this.getCommonService().saveObject(roleFunction);
		   		 if(!bl) return "error";
		   }
	    return SUCCESS;
	}

	
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		/*删除角色之前先删除关联的角色功能对应*/
		String deleteList="select rf from RoleFunction rf where rf.roles.roleId="+roleId;
		List olds=this.getCommonService().findListByHql(deleteList, null);
		for(int i=0;i<olds.size();i++){
			boolean d=this.getCommonService().deleteObject((RoleFunction)olds.get(i));
			if(!d){
				return "error";
			}
		}
		
		/*删除角色之前先删除关联的教师角色对应*/
		String deleteList2="select tr from TeacherRole tr where tr.roles.roleId="+roleId;
		List olds2=this.getCommonService().findListByHql(deleteList2, null);
		for(int i=0;i<olds2.size();i++){
			boolean d2=this.getCommonService().deleteObject((TeacherRole)olds2.get(i));
			if(!d2){
				return "error";
			}
		}
		
		/*删除角色*/
		Roles deleteRoles =new Roles();
		deleteRoles=(Roles)this.getCommonService().findByIntId(roleId, "gdut.jmxywz.been.Roles");
		if(deleteRoles!=null){
			boolean bl=this.getCommonService().deleteObject(deleteRoles);
			return SUCCESS;
		}else{
			return "error";
		}
		
	}

	
	public String subPreUpdate() throws Exception {
		// TODO Auto-generated method stub
		
		roleFunctionSubUpdate=(RoleFunction)this.getCommonService().findByIntId(roleFunctionId, "gdut.jmxywz.been.RoleFunction");

		if(roleFunctionSubUpdate==null){
			return "error";
		}
		return SUCCESS;
	}

	
	public String subUpdate() throws Exception {
		// TODO Auto-generated method stub
		boolean bl=this.getCommonService().updateObject(roleFunctionSubUpdate);
		if(bl)
			return SUCCESS;
		else
			return "error";
	}
	
	
}
